//
//  LoginViewController.m
//  JinYong_iOS_Store
//
//  Created by 王厚一 on 2017/10/18.
//  Copyright © 2017年 why. All rights reserved.
//

#import "LoginViewController.h"
#import "ResetPasswordViewController.h"
#import "MainViewController.h"

@interface LoginViewController ()<UIScrollViewDelegate, UITextFieldDelegate, UIGestureRecognizerDelegate>

@property (nonatomic, strong) UILabel *topTitle;//标题
@property(nonatomic,assign)dispatch_source_t timer;//定时器

@property (nonatomic, strong) UIButton * leftChangeBtn;//选择登录方式按钮
@property (nonatomic, strong) UIButton * rightChangeBtn;

@property (nonatomic,strong) UITextField *accountField;//账号密码框
@property (nonatomic,strong) UITextField *passwordField;
@property (nonatomic, strong) UITextField * accountField2;//另一组账号密码框
@property (nonatomic, strong) UITextField * passwordField2;

@property (nonatomic, strong) UIScrollView * scrollView;//下方的视图切换scroll
@property (nonatomic, strong) UIView * moveView;//选择登录的滑动线
@property (nonatomic, strong) UIView * bigView;//下方整体的大视图

@property (nonatomic,strong) UIButton *loginButton;//登录的按钮
@property (nonatomic,strong) UIButton *loginButton2;

@property (nonatomic,strong) UIButton *VerificationBtn;//验证码

@property (nonatomic, strong) CAGradientLayer * gradientLayer1;//渐变色
@property (nonatomic, strong) CAGradientLayer * gradientLayer2;


@end

@implementation LoginViewController

#pragma mark - ------------------------声明周期-----------------------------
- (void)viewWillDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
    if (self.timer) {
        dispatch_suspend(self.timer);//退出界面时，读秒还在进行中，这时候应该暂停该队列
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.view.backgroundColor = [UIColor whiteColor];
    //设置导航栏
    [self setupNavigationItem];
    //初始化视图
    [self initView];
    [self initLeftView];
    [self initRightView];
    
}

#pragma mark - ----------------------创建视图-------------------------

-(void)setupNavigationItem{
    
    UIView * topView = ({
        UIView * view = [[UIView alloc] initWithFrame:CGRectMake(ActualW(0), ActualH(0), ActualW(375), ActualH(64))];
        view.backgroundColor = RGB(255, 255, 255);
        view;
    });
    [self.view addSubview:topView];
    
    self.topTitle = ({
        UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake((SCREENWidth-ActualW(100))/2, ActualH(20), ActualW(100), ActualH(44))];
        label.textColor = RGB(51, 51, 51);
        label.textAlignment = NSTextAlignmentCenter;
        label.text = @"登录";
        label.font = FontX(FONTSIZE(18));
        label;
    });
    
    [topView addSubview:self.topTitle];
}

//大图
-(void)initView{
    
    UIView * breakView = ({
        UIView * view = [[UIView alloc] initWithFrame:CGRectMake(0, ActualH(63.5), SCREENW, ActualH(10))];
        view.backgroundColor = RGB(240, 240, 240);
        view;
    });
    [self.view addSubview:breakView];
    
    
    //使用 账号密码登录 按钮
    self.leftChangeBtn = ({
        UIButton * button = [[UIButton alloc] initWithFrame:CGRectMake(ActualW(46.5), ActualH(87), ActualW(95), ActualH(15))];
        button.enabled = NO;
        [button addTarget:self action:@selector(changeAction:) forControlEvents:UIControlEventTouchUpInside];
        [button setTitle:@"账号密码登录" forState:UIControlStateNormal];
        [button setTitleColor:RGB(51, 51, 51) forState:UIControlStateNormal];
        button.titleLabel.font = FontX(FONTSIZE(15));
        button;
    });
    [self.view addSubview:self.leftChangeBtn];
    
    
    //使用 手机验证码登录 按钮
    self.rightChangeBtn = ({
        UIButton * button = [[UIButton alloc] initWithFrame:CGRectMake(ActualW(225), ActualH(87), ActualW(110), ActualH(15))];
        [button addTarget:self action:@selector(changeAction:) forControlEvents:UIControlEventTouchUpInside];
        [button setTitle:@"手机验证码登录" forState:UIControlStateNormal];
        [button setTitleColor:RGB(104, 104, 104) forState:UIControlStateNormal];
        button.titleLabel.font = FontX(FONTSIZE(15));
        button;
    });
    [self.view addSubview:self.rightChangeBtn];
    
    //分割线和滑动线
    UIView * breakLine = ({
        UIView * view = [[UIView alloc] initWithFrame:CGRectMake(ActualW(10), ActualH(114), ActualW(355), ActualH(1))];
        view.backgroundColor = RGBA(220, 220, 220, 0.5);
        view;
    });
    [self.view addSubview:breakLine];
    
    self.moveView = ({
        UIView * view = [[UIView alloc] initWithFrame:CGRectMake(ActualW(47.5), CGRectGetMinY(breakLine.frame) - 2, ActualW(92), ActualH(2))];
        view.backgroundColor = RGBA(82, 141, 240, 1);
        view;
    });
    [self.view addSubview:self.moveView];
    
    
    //整体滑动视图
    self.scrollView = ({
        UIScrollView * scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(breakLine.frame)+1, SCREENW, SCREENH - CGRectGetMaxY(breakLine.frame) - 1)];
        scrollView.showsHorizontalScrollIndicator = NO;
        scrollView.pagingEnabled = YES;
        scrollView.delegate = self;
        scrollView.contentSize = CGSizeMake(SCREENW * 2, 0);
        scrollView;
    });
    [self.view addSubview:self.scrollView];
    
    self.bigView = ({
        UIView * view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREENW * 2, SCREENH - CGRectGetMaxY(breakLine.frame)-1)];
        view.backgroundColor = RGB(255, 255, 255);
        view;
    });
    [self.scrollView addSubview:self.bigView];
    
}

- (void)initLeftView {
    //账号密码框
    UILabel * userLabel = ({
        UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(ActualW(10), ActualH(149) - CGRectGetMinY(self.scrollView.frame), ActualW(45), ActualH(15))];
        label.textColor = RGB(73, 73, 73);
        label.text = @"账号";
        label.font = FontX(FONTSIZE(15));
        label;
    });
    [self.bigView addSubview:userLabel];
    
    UILabel * passwdLabel = ({
        UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(ActualW(10), ActualH(192) - CGRectGetMinY(self.scrollView.frame), ActualW(45), ActualH(15))];
        label.textColor = RGB(73, 73, 73);
        label.text = @"密码";
        label.font = FontX(FONTSIZE(15));
        label;
    });
    [self.bigView addSubview:passwdLabel];
    
    //分割线
    UIView * breakLine2 = ({
        UIView * view = [[UIView alloc] initWithFrame:CGRectMake(ActualW(10), ActualH(177) - CGRectGetMinY(self.scrollView.frame), ActualW(355), ActualH(0.5))];
        view.backgroundColor = RGB(220, 220, 220);
        view;
    });
    [self.bigView addSubview:breakLine2];
    
    UIView * breakLine3 = ({
        UIView * view = [[UIView alloc] initWithFrame:CGRectMake(ActualW(10), ActualH(221) - CGRectGetMinY(self.scrollView.frame), ActualW(355), ActualH(0.5))];
        view.backgroundColor = RGB(220, 220, 220);
        view;
    });
    [self.bigView addSubview:breakLine3];
  
    //账号框
    self.accountField = ({
        UITextField * textField = [[UITextField alloc]initWithFrame:CGRectMake(ActualW(101) ,ActualH(145) - CGRectGetMinY(self.scrollView.frame), SCREENW - ActualW(101), ActualH(21.5))];
        textField.backgroundColor = [UIColor whiteColor];
        textField.font = FontX(FONTSIZE(15));
        textField.placeholder = @"请输入管理员绑定手机号";
        textField.textColor = RGBA(73, 73, 73, 1);
        textField.autocapitalizationType = UITextAutocapitalizationTypeNone;//是否首字母大写
        textField.delegate = self;
        textField.returnKeyType = UIReturnKeyNext;//设置键盘按键类型
        textField.clearButtonMode = UITextFieldViewModeWhileEditing;//一键清除按钮
        textField.enablesReturnKeyAutomatically = YES;//这里设置为无文字就灰色不可点
        textField.leftViewMode = UITextFieldViewModeAlways;
        [textField addTarget:self action:@selector(returnOnKeyboard:) forControlEvents:UIControlEventEditingChanged];
        textField;
    });
    [self.bigView addSubview:self.accountField];
    
    //密码框
    self.passwordField = ({
        UITextField * textField = [[UITextField alloc]initWithFrame:CGRectMake(ActualW(101) ,ActualH(188.5) - CGRectGetMinY(self.scrollView.frame), SCREENW - ActualW(101), ActualH(21.5))];
        textField.backgroundColor =[UIColor whiteColor];
        textField.font = FontX(FONTSIZE(15));
        textField.placeholder = @"请输入密码";
        textField.textColor = RGBA(73, 73, 73, 1);
        textField.secureTextEntry = YES;//是否以密码形式显示
        textField.delegate = self;
        textField.returnKeyType = UIReturnKeyDone;
        textField.clearButtonMode = UITextFieldViewModeWhileEditing;
        textField.enablesReturnKeyAutomatically = YES;
        textField.leftViewMode = UITextFieldViewModeAlways;
        [textField addTarget:self action:@selector(returnOnKeyboard:) forControlEvents:UIControlEventEditingChanged];
        textField;
    });
    [self.bigView addSubview:self.passwordField];
    
    //登录按钮登录1
    self.loginButton = ({
        UIButton * button = [[UIButton alloc]initWithFrame:CGRectMake(ActualW(10),ActualH(246) - CGRectGetMinY(self.scrollView.frame) ,ActualW(355), ActualH(40))];
        button.enabled=NO;
        button.layer.cornerRadius = ActualW(4);
        [button addTarget:self action:@selector(loginBtnClick) forControlEvents:UIControlEventTouchUpInside];
        //初始化CAGradientlayer对象，使它的大小为UIView的大小
        self.gradientLayer1 = [CAGradientLayer layer];
        self.gradientLayer1.frame = button.bounds;
        self.gradientLayer1.cornerRadius = ActualW(4);
        //将CAGradientlayer对象添加在我们要设置背景色的视图的layer层
        [button.layer addSublayer:self.gradientLayer1];
        //设置渐变区域的起始和终止位置（范围为0-1）
        self.gradientLayer1.startPoint = CGPointMake(0, 0);
        self.gradientLayer1.endPoint = CGPointMake(1, 0);
        //设置颜色数组
        self.gradientLayer1.colors = @[(__bridge id)RGB(192, 192, 192).CGColor, (__bridge id)RGB(192, 192, 192).CGColor];
        //设置颜色分割点（范围：0-1）
        self.gradientLayer1.locations = @[@(0.0f), @(1.0f)];
        button;
    });
    [self.bigView addSubview:self.loginButton];
    
    UILabel * denglu = ({
        UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(ActualW(0), ActualH(0), ActualW(355), ActualH(40))];
        label.textColor = RGB(255, 255, 255);
        label.textAlignment = NSTextAlignmentCenter;
        label.text = @"登录";
        label.font = FontX(FONTSIZE(18));
        label.backgroundColor = [UIColor clearColor];
        label;
    });
    [self.loginButton addSubview:denglu];
    
    UILabel * forgetPasswd = ({
        UILabel * label = [[UILabel alloc]initWithFrame:CGRectMake(ActualW(304.5), ActualH(303.5) - CGRectGetMinY(self.scrollView.frame),ActualW(60.5), ActualH(12))];
        label.textColor = RGB(51, 51, 51);
        label.textAlignment = NSTextAlignmentRight;
        label.text = @"忘记密码?";
        label.font = FontX(FONTSIZE(12));
        label.userInteractionEnabled = YES;
        label;
    });
    [self.bigView addSubview:forgetPasswd];
    
    UIButton * forgetBtn = ({
        UIButton * button = [[UIButton alloc] initWithFrame:CGRectMake(ActualW(304.5), CGRectGetMaxY(self.loginButton.frame), ActualW(60.5), ActualH(50))];
        [button addTarget:self action:@selector(forgetClick) forControlEvents:UIControlEventTouchUpInside];
        button;
    });
    [self.bigView addSubview:forgetBtn];
    
    
    //添加手势，点击屏幕其他区域关闭键盘的操作
    UITapGestureRecognizer *gesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hidenKeyboard)];
    gesture.numberOfTapsRequired = 1;
    gesture.delegate = self;
    [self.view addGestureRecognizer:gesture];

}

//另一个视图
- (void)initRightView {
    //账号密码框
    UILabel * userLabel = ({
        UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(ActualW(10)+SCREENW, ActualH(149) - CGRectGetMinY(self.scrollView.frame), ActualW(50), ActualH(15))];
        label.textColor = RGB(73, 73, 73);
        label.text = @"手机号";
        label.font = FontX(FONTSIZE(15));
        label;
    });
    [self.bigView addSubview:userLabel];
    
    UILabel * passwdLabel = ({
        UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(ActualW(10)+SCREENW, ActualH(192) - CGRectGetMinY(self.scrollView.frame), ActualW(50), ActualH(15))];
        label.textColor = RGB(73, 73, 73);
        label.text = @"验证码";
        label.font = FontX(FONTSIZE(15));
        label;
    });
    [self.bigView addSubview:passwdLabel];
    
    //分割线
    UIView * breakLine2 = ({
        UIView * view = [[UIView alloc] initWithFrame:CGRectMake(ActualW(10)+SCREENW, ActualH(177) - CGRectGetMinY(self.scrollView.frame), ActualW(355), ActualH(0.5))];
        view.backgroundColor = RGBA(220, 220, 220, 1);
        view;
    });
    [self.bigView addSubview:breakLine2];
    
    UIView * breakLine3 = ({
        UIView * view = [[UIView alloc] initWithFrame:CGRectMake(ActualW(10)+SCREENW, ActualH(221) - CGRectGetMinY(self.scrollView.frame), ActualW(355), ActualH(0.5))];
        view.backgroundColor = RGBA(220, 220, 220, 1);
        view;
    });
    [self.bigView addSubview:breakLine3];
    
    //账号框
    self.accountField2 = ({
        UITextField * textField = [[UITextField alloc]initWithFrame:CGRectMake(ActualW(101)+SCREENW ,ActualH(145) - CGRectGetMinY(self.scrollView.frame), SCREENW - ActualW(101), ActualH(21.5))];
        textField.backgroundColor = [UIColor whiteColor];
        textField.font = FontX(FONTSIZE(15));
        textField.placeholder = @"请输入管理员绑定手机号";
        textField.textColor = RGBA(73, 73, 73, 1);
        textField.autocapitalizationType = UITextAutocapitalizationTypeNone;//是否首字母大写
        textField.delegate = self;
        textField.returnKeyType = UIReturnKeyNext;//设置键盘按键类型
        textField.clearButtonMode = UITextFieldViewModeWhileEditing;//一键清除按钮
        textField.enablesReturnKeyAutomatically = YES;//这里设置为无文字就灰色不可点
        textField.leftViewMode = UITextFieldViewModeAlways;
        [textField addTarget:self action:@selector(returnOnKeyboard:) forControlEvents:UIControlEventEditingChanged];
        textField;
    });
    [self.bigView addSubview:self.accountField2];
    
    //密码框
    self.passwordField2 = ({
        UITextField * textField = [[UITextField alloc]initWithFrame:CGRectMake(ActualW(101)+SCREENW ,ActualH(188.5) - CGRectGetMinY(self.scrollView.frame), SCREENW - ActualW(101), ActualH(21.5))];
        textField.backgroundColor =[UIColor whiteColor];
        textField.font = FontX(FONTSIZE(15));
        textField.placeholder = @"请输入验证码";
        textField.textColor = RGBA(73, 73, 73, 1);
        textField.delegate = self;
        textField.returnKeyType = UIReturnKeyDone;
        textField.clearButtonMode = UITextFieldViewModeWhileEditing;
        textField.enablesReturnKeyAutomatically = YES;
        textField.leftViewMode = UITextFieldViewModeAlways;
        [textField addTarget:self action:@selector(returnOnKeyboard:) forControlEvents:UIControlEventEditingChanged];
        textField;
    });
    [self.bigView addSubview:self.passwordField2];
    
    //登录按钮登录2
    self.loginButton2 = ({
        UIButton * button = [[UIButton alloc]initWithFrame:CGRectMake(ActualW(10)+SCREENW,ActualH(246) - CGRectGetMinY(self.scrollView.frame) ,ActualW(355), ActualH(40))];
        button.enabled=NO;
        button.layer.cornerRadius = ActualW(4);
        [button addTarget:self action:@selector(loginBtn2Click) forControlEvents:UIControlEventTouchUpInside];
        //初始化CAGradientlayer对象，使它的大小为UIView的大小
        self.gradientLayer2 = [CAGradientLayer layer];
        self.gradientLayer2.frame = button.bounds;
        self.gradientLayer2.cornerRadius = ActualW(4);
        //将CAGradientlayer对象添加在我们要设置背景色的视图的layer层
        [button.layer addSublayer:self.gradientLayer2];
        //设置渐变区域的起始和终止位置（范围为0-1）
        self.gradientLayer2.startPoint = CGPointMake(0, 0);
        self.gradientLayer2.endPoint = CGPointMake(1, 0);
        //设置颜色数组
        self.gradientLayer2.colors = @[(__bridge id)RGB(192, 192, 192).CGColor, (__bridge id)RGB(192, 192, 192).CGColor];
        //设置颜色分割点（范围：0-1）
        self.gradientLayer2.locations = @[@(0.0f), @(1.0f)];
        button;
    });
    [self.bigView addSubview:self.loginButton2];
    
    UILabel * denglu = ({
        UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(ActualW(0), ActualH(0), ActualW(355), ActualH(40))];
        label.textColor = RGB(255, 255, 255);
        label.textAlignment = NSTextAlignmentCenter;
        label.text = @"登录";
        label.font = FontX(FONTSIZE(18));
        label.backgroundColor = [UIColor clearColor];
        label;
    });
    [self.loginButton2 addSubview:denglu];
    
    //联系客服
    UILabel * kefu = [[UILabel alloc] initWithFrame:CGRectMake(ActualW(10)+SCREENW, ActualH(295.5) - CGRectGetMinY(self.scrollView.frame), ActualW(200), ActualH(10))];
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:@"遇到问题？请联系客服。"];
    [attributedString addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithWhite:170.0f / 255.0f alpha:1.0f] range:NSMakeRange(0, 5)];
    [attributedString addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithWhite:51.0f / 255.0f alpha:1.0f] range:NSMakeRange(5, 5)];
    
    kefu.attributedText = attributedString;
    kefu.font = FontX(FONTSIZE(12));
    [self.bigView addSubview:kefu];
    
    UIButton * kefuBtn = ({
        UIButton * button = [[UIButton alloc] initWithFrame:CGRectMake(ActualW(10)+SCREENW, CGRectGetMaxY(self.loginButton2.frame), ActualW(200), ActualH(50))];
        [button addTarget:self action:@selector(enterKefu) forControlEvents:UIControlEventTouchUpInside];
        button;
    });
    [self.bigView addSubview:kefuBtn];
    
    //验证码
    self.VerificationBtn = ({
        UIButton * button = [[UIButton alloc] initWithFrame:CGRectMake(ActualW(256)+SCREENW, ActualH(188.5) - CGRectGetMinY(self.scrollView.frame), ActualW(109), ActualH(21.5))];
        button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
        [button addTarget:self action:@selector(verificationAction) forControlEvents:UIControlEventTouchUpInside];
        [button setTitle:@"发送验证码" forState:UIControlStateNormal];
        [button setTitleColor:RGB(102, 102, 102) forState:UIControlStateNormal];
        button.titleLabel.font = FontX(FONTSIZE(12));
        button;
    });
    [self.bigView addSubview:self.VerificationBtn];
}

#pragma mark - ------------------------点击事件---------------------------------
- (void)loginBtnClick {
    
    [[URLFather sharedInstance] loginWithMobile:self.accountField.text password:self.passwordField.text success:^(NSDictionary *result) {
        
        if ([Pub resultSuccess:result]) {
            
            NSDictionary *data=[result objectForKey:@"data"];
 
            [NSUDUD setBool:YES forKey:kUserLoginStatus];
            [NSUDUD setValue:[NSString stringWithFormat:@"%@",[data objectForKey:@"cookie"]] forKey:kKey];
            [NSUDUD setValue:[NSString stringWithFormat:@"%@",[data objectForKey:@"user_id"]] forKey:kUserId];
            [NSUDUD setValue:@"0" forKey:kOrderUnfinishedNum];
            [NSUDUD setValue:@"0" forKey:kOrderUnfinishedNum];
            
            NSLog(@"登录成功，获得登录状态 kUserLoginStatus 为 %d",kUserLoginResult);
            [NSUDUD synchronize];
            
            sendNotification(NOTIFICATION_LOGINSUCCESS)//登录成功通知
            //设置别名
            [JPUSHService setAlias:data[@"user_id"] completion:^(NSInteger iResCode, NSString *iAlias, NSInteger seq) {
                NSLog(@"%ld----%@-----%ld", iResCode, iAlias, seq);
            } seq:0];
            
            kShowSuccess(@"登录成功");
            if (self.isReplace) {
                NSArray * controllerArray = self.navigationController.viewControllers;
                [self.navigationController popToViewController:controllerArray.firstObject animated:YES];
            }else {
                [self dismissViewControllerAnimated:YES completion:nil];
            }
        }
        else
        {
            kShowText(result[@"msg"]);
        }
    } failure:^(NSError *error) {
        kShowError(@"请求失败");
    }];

}

- (void)loginBtn2Click {
    [[URLFather sharedInstance] loginWithMobile:self.accountField2.text code:self.passwordField2.text success:^(NSDictionary *result) {
        
        if ([Pub resultSuccess:result]) {
            
            NSLog(@"result - %@", result);
            NSDictionary *data=[result objectForKey:@"data"];
            
            [NSUDUD setBool:YES forKey:kUserLoginStatus];
            [NSUDUD setValue:[NSString stringWithFormat:@"%@",[data objectForKey:@"cookie"]] forKey:kKey];
            [NSUDUD setValue:[NSString stringWithFormat:@"%@",[data objectForKey:@"user_id"]] forKey:kUserId];
            
            [NSUDUD setValue:@"0" forKey:kOrderUnfinishedNum];
            [NSUDUD setValue:@"0" forKey:kOrderUnfinishedNum];
            
            NSLog(@"登录成功，获得登录状态 kUserLoginStatus 为 %d",kUserLoginResult);
            [NSUDUD synchronize];
            
            sendNotification(NOTIFICATION_LOGINSUCCESS)//登录成功通知
            //设置别名
            [JPUSHService setAlias:data[@"user_id"] completion:^(NSInteger iResCode, NSString *iAlias, NSInteger seq) {
                NSLog(@"%ld----%@-----%ld", iResCode, iAlias, seq);
            } seq:0];
            
            kShowSuccess(@"登录成功");
            //如果通过退出登录后 重新进入主页
            if (self.isReplace) {
                NSArray * controllerArray = self.navigationController.viewControllers;
                for (UIViewController * controller in controllerArray) {
                    if ([controller isEqual:[MainViewController class]]) {
                        MainViewController * vc = (MainViewController *)controller;
                        [self.navigationController popToViewController:vc animated:YES];
                    }
                }
            }else {
                [self dismissViewControllerAnimated:YES completion:nil];
            }        }
        else
        {
            kShowText(result[@"msg"]);
        }
    } failure:^(NSError *error) {
        kShowError(@"请求失败");
    }];
}

- (void)forgetClick {
    ResetPasswordViewController * vc = [[ResetPasswordViewController alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)enterKefu {
    NSLog(@"客服");
}

#pragma mark - --------------------------验证码--------------------------------
-(void)verificationAction{
    if (!(_accountField2.text.length==11)) {
        kShowText(@"输入长度不正确");
        return;
    }
    
    [[URLFather sharedInstance] getLoginCodeWithMobile:self.accountField2.text success:^(NSDictionary *result) {
        
        if ([Pub resultSuccess:result]) {
            kShowText(@"验证码已发送");
            [self getStartTime];
        }else{
            kShowText([result objectForKey:@"msg"]);
        }
    } failure:^(NSError *error) {
        kShowError(@"请求失败");
    }];

}

-(void)getStartTime{
    
    __block int timeout=60; //倒计时时间
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_source_t timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0,queue);
    self.timer=timer;
    dispatch_source_set_timer(timer,dispatch_walltime(NULL, 0),1.0*NSEC_PER_SEC, 0); //每秒执行
    
    @WeakObj(self);
    dispatch_source_set_event_handler(timer, ^{
        
        if(timeout<=0){ //倒计时结束，关闭
            
            dispatch_source_cancel(timer);
            
            dispatch_async(dispatch_get_main_queue(), ^{
                
                //设置界面的按钮显示 根据自己需求设置
                [selfWeak.VerificationBtn setTitle:@"发送验证码" forState:UIControlStateNormal];
                selfWeak.VerificationBtn.userInteractionEnabled = YES;
                
            });
            
        }else{
            int seconds = timeout % 61;
            NSString *strTime = [NSString stringWithFormat:@"%.2d", seconds];
            dispatch_async(dispatch_get_main_queue(), ^{
                [selfWeak.VerificationBtn setTitle:[NSString stringWithFormat:@"%@S",strTime] forState:UIControlStateNormal];
                selfWeak.VerificationBtn.userInteractionEnabled = NO;
                
            });
            timeout--;
        }
        
    });
    
    dispatch_resume(timer);
}

#pragma mark - ------------------------滑动线---------------------------
- (void)changeAction:(UIButton *)btn {
    if (btn == self.rightChangeBtn) {
        self.leftChangeBtn.enabled = YES;
        self.rightChangeBtn.enabled = NO;
        [UIView animateWithDuration:0.5 animations:^{
            self.scrollView.contentOffset = CGPointMake(SCREENW, 0);
        } completion:^(BOOL finished) {
            [UIView animateWithDuration:0.3 animations:^{
                self.moveView.frame = CGRectMake(ActualW(230.5), self.moveView.frame.origin.y, self.moveView.frame.size.width, self.moveView.frame.size.height);
            } completion:^(BOOL finished) {
                
            }];
        }];
    }else {
        self.rightChangeBtn.enabled = YES;
        self.leftChangeBtn.enabled = NO;
        [UIView animateWithDuration:0.5 animations:^{
            self.scrollView.contentOffset = CGPointMake(0, 0);
        } completion:^(BOOL finished) {
            [UIView animateWithDuration:0.3 animations:^{
                self.moveView.frame = CGRectMake(ActualW(47.5), self.moveView.frame.origin.y, self.moveView.frame.size.width, self.moveView.frame.size.height);
            } completion:^(BOOL finished) {
                
            }];
        }];
    }
}

#pragma mark - --------------------------视图移动-----------------------------

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    if (scrollView == self.scrollView) {
        if (scrollView.contentOffset.x == SCREENW) {
            self.leftChangeBtn.enabled = YES;
            self.rightChangeBtn.enabled = NO;
            [UIView animateWithDuration:0.3 animations:^{
                self.moveView.frame = CGRectMake(ActualW(230.5), self.moveView.frame.origin.y, self.moveView.frame.size.width, self.moveView.frame.size.height);
            } completion:^(BOOL finished) {
                
            }];
            
        }
        if (scrollView.contentOffset.x == 0) {
            self.rightChangeBtn.enabled = YES;
            self.leftChangeBtn.enabled = NO;
            [UIView animateWithDuration:0.3 animations:^{
                self.moveView.frame = CGRectMake(ActualW(47.5), self.moveView.frame.origin.y, self.moveView.frame.size.width, self.moveView.frame.size.height);
            } completion:^(BOOL finished) {
                
            }];
        }
    }
}

#pragma mark - -----------------------------检测是否可以登录--------------------------
-(void)check{
    
    if (self.accountField.text.length > 0 && self.passwordField.text.length > 0) {
        self.gradientLayer1.colors = @[(__bridge id)RGB(82, 137, 240).CGColor, (__bridge id)RGB(86, 176, 224).CGColor];
        self.loginButton.enabled=YES;
    }else{
        self.gradientLayer1.colors = @[(__bridge id)RGB(192, 192, 192).CGColor, (__bridge id)RGB(192, 192, 192).CGColor];
        self.loginButton.enabled=NO;
    }
    
    if (self.accountField2.text.length > 0 && self.passwordField2.text.length > 0) {
        self.gradientLayer2.colors = @[(__bridge id)RGB(82, 137, 240).CGColor, (__bridge id)RGB(86, 176, 224).CGColor];
        self.loginButton2.enabled=YES;
    }else{
        self.gradientLayer2.colors = @[(__bridge id)RGB(192, 192, 192).CGColor, (__bridge id)RGB(192, 192, 192).CGColor];
        self.loginButton2.enabled=NO;
    }
}


#pragma mark - --------------------------键盘操作---------------------------
-(void)hidenKeyboard{
    
    [_accountField resignFirstResponder];
    [_passwordField resignFirstResponder];
}

- (void)returnOnKeyboard:(UITextField *)sender
{
    
    if (_accountField.text.length != 0 && _passwordField.text.length !=0 ) {
        _loginButton.enabled = YES;
    } else {
        _loginButton.enabled = NO;
        
    }
    if (_accountField2.text.length != 0 && _passwordField2.text.length !=0 ) {
        _loginButton2.enabled = YES;
    } else {
        _loginButton2.enabled = NO;
        
    }
    
    [self check];
}

-(void)textFieldDidChange:(UITextField *)textField{
    [self check];
}

-(BOOL)textFieldShouldReturn:(UITextField *)textField{
    
    if (textField == _accountField) {
        return [_passwordField becomeFirstResponder];
    }else{
        
        [self hidenKeyboard];
        return [_passwordField resignFirstResponder];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
